package com.cain.calendar.activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.cain.calendar.R;
import com.cain.calendar.bean.Event;
import com.cain.calendar.dao.CalendarDAO;
import com.cain.calendar.tool.Constant;
import com.cain.calendar.view.CustomDialog;

import java.util.Date;

public class EventActivity extends AppCompatActivity {

    private TextView nameText;
    private TextView dateText;
    private TextView timeText;
    private TextView tagText;
    private TextView remarkText;

    private Event event;

    private static final int REQUEST_CODE_EDIT = 100;
    public static final int RESULT_CODE_UPDATE = 100;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        event = (Event) getIntent().getSerializableExtra("event");
        if (event == null)
            event = new Event();

        setContentView(R.layout.activity_event);
        nameText = (TextView) findViewById(R.id.event_name);
        dateText = (TextView) findViewById(R.id.event_date);
        timeText = (TextView) findViewById(R.id.event_time);
        tagText = (TextView) findViewById(R.id.event_tag);
        remarkText = (TextView) findViewById(R.id.event_remark);

        nameText.setText(event.getName());
        Date date = event.getTime();
        dateText.setText(String.format("%04d/%02d/%02d %s",
                date.getYear() + 1900,
                date.getMonth() + 1,
                date.getDate(),
                Constant.weekdayName[date.getDay()]));
        timeText.setText(String.format("%02d:%02d", date.getHours(), date.getMinutes()));
        tagText.setText(event.getTag());
        remarkText.setText(event.getRemark());
    }

    /**
     * 返回
     * @param view
     */
    public void back(View view) {
        finish();
    }

    /**
     * 删除日程
     * @param view
     */
    public void delete(View view) {
        CustomDialog.Builder builder = new CustomDialog.Builder(this);
        builder.setMessage("真的要删除该日程吗？");
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                CalendarDAO.deleteEvent(event);
                setResult(RESULT_CODE_UPDATE);
                dialog.dismiss();
                finish();
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        builder.create().show();
    }

    /**
     * 编辑日程
     * @param view
     */
    public void edit(View view) {
        Intent intent = new Intent(this, EditActivity.class);
        intent.putExtra("event", event);
        startActivityForResult(intent, REQUEST_CODE_EDIT);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            case REQUEST_CODE_EDIT:
                switch (resultCode) {
                    case EditActivity.RESULT_CODE_UPDATE:
                        setResult(RESULT_CODE_UPDATE);
                        finish();
                        break;
                }
                break;
        }
    }
}
